/*
 * Copyright (C) 2007-2010 Geometer Plus <contact@geometerplus.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

package org.geometerplus.fbreader.formats.fb2;

import org.geometerplus.fbreader.Paths;
import org.geometerplus.zlibrary.core.image.ZLSingleImage;

import java.io.*;

final class Base64EncodedImage extends ZLSingleImage {
    private static int ourCounter;

    static void resetCounter() {
        ourCounter = 0;
    }

    private final String myDirName;
    private final int myFileNumber;
    private OutputStreamWriter myStreamWriter;
    private boolean myIsDecoded;

    public Base64EncodedImage(String contentType) {
        // TODO: use contentType
        super(contentType);
        myDirName = Paths.cacheDirectory().intern();
        new File(myDirName).mkdirs();
        myFileNumber = ourCounter++;
        try {
            myStreamWriter = new OutputStreamWriter(new FileOutputStream(myDirName + "/image" + myFileNumber), "UTF-8");
        } catch (IOException e) {
        }
    }

    private static byte decodeByte(byte encodedByte) {
        switch (encodedByte) {
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F':
            case 'G':
            case 'H':
            case 'I':
            case 'J':
            case 'K':
            case 'L':
            case 'M':
            case 'N':
            case 'O':
            case 'P':
            case 'Q':
            case 'R':
            case 'S':
            case 'T':
            case 'U':
            case 'V':
            case 'W':
            case 'X':
            case 'Y':
            case 'Z':
                return (byte) (encodedByte - 'A');
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f':
            case 'g':
            case 'h':
            case 'i':
            case 'j':
            case 'k':
            case 'l':
            case 'm':
            case 'n':
            case 'o':
            case 'p':
            case 'q':
            case 'r':
            case 's':
            case 't':
            case 'u':
            case 'v':
            case 'w':
            case 'x':
            case 'y':
            case 'z':
                return (byte) (encodedByte - 'a' + 26);
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                return (byte) (encodedByte - '0' + 52);
            case '+':
                return 62;
            case '/':
                return 63;
            case '=':
                return 64;
        }
        return -1;
    }

    public byte[] byteData() {
        try {
            decode();
            final File file = new File(myDirName + "/dimage" + myFileNumber);
            final byte[] data = new byte[(int) file.length()];
            final FileInputStream stream = new FileInputStream(file);
            stream.read(data);
            stream.close();
            return data;
        } catch (IOException e) {
            return null;
        }
    }

    private void decode() throws IOException {
        if (myIsDecoded) {
            return;
        }
        myIsDecoded = true;

        int dataLength;
        byte[] encodedData;
        FileOutputStream outputStream;

        final File file = new File(myDirName + "/image" + myFileNumber);
        outputStream = new FileOutputStream(myDirName + "/dimage" + myFileNumber);
        dataLength = (int) file.length();
        final FileInputStream inputStream = new FileInputStream(file);
        encodedData = new byte[dataLength];
        inputStream.read(encodedData);
        inputStream.close();
        file.delete();

        final byte[] data = new byte[dataLength * 3 / 4 + 4];
        int dataPos = 0;
        for (int pos = 0; pos < dataLength;) {
            byte n0 = -1, n1 = -1, n2 = -1, n3 = -1;
            while ((pos < dataLength) && (n0 == -1)) {
                n0 = decodeByte(encodedData[pos++]);
            }
            while ((pos < dataLength) && (n1 == -1)) {
                n1 = decodeByte(encodedData[pos++]);
            }
            while ((pos < dataLength) && (n2 == -1)) {
                n2 = decodeByte(encodedData[pos++]);
            }
            while ((pos < dataLength) && (n3 == -1)) {
                n3 = decodeByte(encodedData[pos++]);
            }
            data[dataPos++] = (byte) (n0 << 2 | n1 >> 4);
            data[dataPos++] = (byte) (((n1 & 0xf) << 4) | ((n2 >> 2) & 0xf));
            data[dataPos++] = (byte) (n2 << 6 | n3);
        }
        outputStream.write(data, 0, dataPos);
        outputStream.close();
    }

    void addData(char[] data, int offset, int length) {
        if (myStreamWriter != null) {
            try {
                myStreamWriter.write(data, offset, length);
            } catch (IOException e) {
            }
        }
    }

    void close() {
        try {
            myStreamWriter.close();
        } catch (IOException e) {
        }
    }
}
